// 登录接口并导出
import type { CommonResult, CommonPage } from '@/models/request'
import type { Role } from '@/models/role'
import type { UserInfo, Token, ReqUserList, User } from '@/models/user'
import request, { ContentTypeEnum } from '@/utils/request'
export const login = async (username: string, password: string) => request.post<CommonResult<Token>>('/admin/login', { username, password })

export const getUserInfo = () => {
  return request.get<CommonResult<UserInfo>>('/admin/info')
}

export function logout() {
  return request<CommonResult>({
    url: '/admin/logout',
    method: 'post',
  })
}

export function fetchList(params: ReqUserList) {
  return request<CommonPage<User>>({
    url: '/admin/list',
    method: 'get',
    params: params,
  })
}

export function createAdmin(data: User) {
  return request<CommonResult>({
    url: '/admin/register',
    method: 'post',
    data: data,
  })
}

export function updateAdmin(id: number, data: User) {
  return request<CommonResult>({
    url: '/admin/update/' + id,
    method: 'post',
    data: data,
  })
}

export function updateStatus(id: number, params: { status: number }) {
  return request<CommonResult>({
    url: '/admin/updateStatus/' + id,
    method: 'post',
    params: params,
  })
}

export function deleteAdmin(id: number) {
  return request<CommonResult>({
    url: '/admin/delete/' + id,
    method: 'post',
  })
}

export function getRoleByAdmin(id: number) {
  return request<CommonResult<Role[]>>({
    url: '/admin/role/' + id,
    method: 'get',
  })
}

export function allocRole(data: { adminId: number; roleIds: string }) {
  return request<CommonResult>({
    url: '/admin/role/update',
    method: 'post',
    data: data,
    headers: {
      'Content-Type': ContentTypeEnum.form,
    },
  })
}

export function adminDetail(id: number) {
  return request<CommonResult<User>>({
    url: '/admin/' + id,
    method: 'get',
  })
}
